#include <Windows.h>
#include <WindowsX.h>
#include <tchar.h>
#include "resource.h"
#include <psapi.h>
#include <TlHelp32.h>
#pragma comment(lib, "Psapi.lib")

void AddTextToEdit(HWND hEdit, TCHAR* szFormat, ...)
{
	va_list vaList;
	va_start(vaList, szFormat);
	TCHAR szTmp[1024 * 40];
	Edit_GetText(hEdit, szTmp, _countof(szTmp));
	_vstprintf(_tcschr(szTmp, _T('\0')), szFormat, vaList);
	Edit_SetText(hEdit, szTmp);
	va_end(vaList);
	return;
}

void UpdateProcessList(HWND hCmbProcess)
{
	DWORD dwProcessIDs[1024];
	DWORD dwCount = 0;
	EnumProcesses(dwProcessIDs, 1024, &dwCount);

	for (int ii = ComboBox_GetCount(hCmbProcess) - 1; ii >= 0; ii--)
	{
		ComboBox_DeleteString(hCmbProcess, ii);
	}
	
	HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if (INVALID_HANDLE_VALUE != hSnapshot)
	{
		PROCESSENTRY32 pe = {sizeof(PROCESSENTRY32)};
		if (Process32First(hSnapshot, &pe))
		{
			do {
				UINT nIndex = ComboBox_AddString(hCmbProcess, pe.szExeFile);
				ComboBox_SetItemData(hCmbProcess, nIndex, pe.th32ProcessID);
			} while (Process32Next(hSnapshot, &pe));
		}		
		ComboBox_SetCurSel(hCmbProcess, 0);
		CloseHandle(hSnapshot);
	}

	return;
}

int CALLBACK DlgProc(HWND hDlg, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
	static HWND hCmbProcess = 0;
	switch (nMsg)
	{
		case WM_INITDIALOG:
		{
			SetWindowText(hDlg, _T("Demo0081"));

			UINT nWidth		= GetSystemMetrics(SM_CXSCREEN);
			UINT nHeight	= GetSystemMetrics(SM_CYSCREEN);
			RECT rtClient;
			GetClientRect(hDlg, &rtClient);
			SetWindowPos(hDlg, NULL, (nWidth - (rtClient.right - rtClient.left)) / 2, (nHeight - (rtClient.bottom - rtClient.top)) / 2, 0, 0, SWP_NOSIZE);
			
			hCmbProcess = GetDlgItem(hDlg, IDC_CMBPROCESS);
			UpdateProcessList(hCmbProcess);
			SendMessage(hDlg, WM_COMMAND, MAKEWPARAM(IDC_CMBPROCESS, CBN_SELCHANGE), 0);

			break;
		}
		case WM_COMMAND:
		{
			if (HIWORD(wParam) == CBN_SELCHANGE)
			{
				DWORD th32ProcessID = ComboBox_GetItemData(hCmbProcess, ComboBox_GetCurSel(hCmbProcess));
				HANDLE hSnapThread = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, th32ProcessID);
				if (INVALID_HANDLE_VALUE != hSnapThread)
				{
					THREADENTRY32 te32 = {sizeof(te32)};
					if (Thread32First(hSnapThread, &te32))
					{
						SetWindowText(GetDlgItem(hDlg, IDC_EDTINFO), _T(""));

						DWORD nCount = 0;
						do {
							if (th32ProcessID == te32.th32OwnerProcessID)
							{
								AddTextToEdit(GetDlgItem(hDlg, IDC_EDTINFO),
											  _T("Thread: %d\r\n")
											  _T("---------------------------------------\r\n")
											  _T("%-20s   0x%06X(%d)\r\n")
											  _T("%-20s   0x%06X(%d)\r\n")
											  _T("%-20s   %d\r\n\r\n"),
											  nCount++,
											  _T("Main Thread ID:"), te32.th32ThreadID, te32.th32ThreadID, 	
											  _T("Parent Thread ID:"), te32.th32OwnerProcessID, te32.th32OwnerProcessID,
											  _T("Priority Level:"), te32.tpBasePri);
							}
						} while (Thread32Next(hSnapThread, &te32));
					}

					CloseHandle(hSnapThread);
				}
			}

			break;
		}
	
		case WM_CLOSE:
		{
			PostQuitMessage(0);
			break;
		}
	}

	return 0;
}

BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR szCmdLine, int nShowCmd)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG), NULL, DlgProc);

	return TRUE;
}